<?php 
class AccountController extends Zend_Controller_Action{

	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
	}

	public function indexAction() {
		$mAccount = new Default_Model_Account();
		if ($mAccount->hasIdentity()) {
			$this->view->fullName = $mAccount->getName();
		} else {
			$this->_redirect('/account/login/');
		}
	}
	
	public function registerAction() {
		if($this->_request->isPost()) {
			$mAccount = new Default_Model_Account();
			$arrCkEmail = $mAccount->checkExistEmail($_POST['email']);
			if(count($arrCkEmail) != 0) {
				// existing email
				$this->_redirect('/account/register/?ERR_CODE=300');
			} else {
				$strNickname = "";
				if(isset($_POST['nickname'])) {
					$strNickname = trim($_POST['nickname']);
				}
				$arrParam = array(
						'Email' => $_POST['email'],
						'NickName' => $strNickname,
						'Password' => md5($_POST['password'])
						);
			}
		}
		
	}
	
	public function loginAction(){
		$mAccount = new Default_Model_Account();
		if(!$mAccount->hasIdentity()) {
			if ($this->_request->isPost()) {
				// redirect processing
				if ($mAccount->login($this->_arrParam)) {
 					if($mAccount->isAdmin()) {
 						if(isset($this->_arrParam['returnurl'])) {
 							$this->_redirect($this->_arrParam['returnurl']);
 						} else {
 							$this->_redirect('/admin/');
 						}
 					} else {
 						if(isset($this->_arrParam['returnurl'])) {
 							$this->_redirect($this->_arrParam['returnurl']);
 						} else {
 							$this->_redirect("/");
 						} 						
 					}
				} else {
					$this->_redirect('/account/login/?ERR_CODE=100');
				}
			}
		} else {
			$this->_redirect($referrerUrl);
		}
	}
	public function logoutAction() {
		$mAccount = new Default_Model_Account();
		if (!$mAccount->hasIdentity()) {
			$this->_redirect('/account/');
		} else {
			$mAccount->logout();
			if(isset($this->_arrParam['returnurl'])) {
				$this->_redirect($this->_arrParam['returnurl']);
			} else {
				$this->_redirect("/");
			}
		}
	}

}